package com.xiaodou.mjf.web.console.staging.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaodou.mjf.application.core.repository.mybatis.LoanMapper;
import com.xiaodou.mjf.application.core.service.LoanService;
import com.xiaodou.mjf.application.fee.service.LoanMasterService;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.Loan;
import com.xiaodou.mjf.entity.fee.LoanMaster;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;

/**
 * 分期申请Controller
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("staging/apply")
public class ApplyProductController extends ApplicationController {

	@Resource
	LoanService loanService;
	@Resource 
	LoanMasterService loanMasterService;
	@Resource
	LoanMapper loanMapper;
	
	
	@RequestMapping("")
	public String apply(Loan loan) {

		return "staging/apply/list";
	}

	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(Loan loan) {
    	if(loan.getStart()==1) {
    		loan.setStart(0);
		} else {
			loan.setStart(loan.getStart()-1);
		}
		Integer count = loanMapper.getAllCount(loan);
		List<Loan> loanList = loanMapper.getAllLoan(loan);
    	return page(count, loanList);
	}

	@RequestMapping(value = { "form", "form/{id}" }, method = RequestMethod.GET)
	public String form(Model model, @PathVariable String id) {
		try {
			Loan loan = loanService.get(id);
			LoanMaster loanMaster = loanMasterService.findByLoanId(id);
			if(loanMaster!=null) {
				loan.setAccountNumber(loanMaster.getAccountNumber());
			}
			model.addAttribute("loan", loan);
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}

		return "staging/apply/form";
	}

}
